<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>反转单向链表 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .step-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2px;
            border-radius: 16px;
            transition: transform 0.3s ease;
        }
        
        .step-card:hover {
            transform: scale(1.05);
        }
        
        .step-content {
            background: white;
            color: #333;
            border-radius: 14px;
            padding: 24px;
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 700;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-20px); }
        }
        
        .first-letter {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        pre code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }
        
        .keyword { color: #569cd6; }
        .type { color: #4ec9b0; }
        .function { color: #dcdcaa; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .number { color: #b5cea8; }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-link text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6">反转单向链表</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                掌握经典算法，优雅地颠倒数据结构的方向
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="first-letter">反</span>转单向链表是一项经典的链表操作。其目的是将链表中所有节点的指向方向颠倒，使得原来的尾节点变成头节点，原来的头节点变成尾节点。这个算法不仅是面试中的常客，更是理解指针操作和数据结构的重要基石。
            </p>
        </div>

        <!-- Visual Representation -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-center">
                <i class="fas fa-diagram-project mr-3 text-purple-600"></i>
                <span class="highlight-text">算法可视化</span>
            </h2>
            <div class="mermaid">
                graph LR
                    subgraph "原始链表"
                        A1[1] --> B1[2]
                        B1 --> C1[3]
                        C1 --> D1[4]
                        D1 --> E1[5]
                        E1 --> F1[null]
                    end
                    
                    subgraph "反转后链表"
                        F2[null] --> E2[5]
                        E2 --> D2[4]
                        D2 --> C2[3]
                        C2 --> B2[2]
                        B2 --> A2[1]
                    end
                    
                    style A1 fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                    style E2 fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Steps Section -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-center">
                <i class="fas fa-list-check mr-3 text-purple-600"></i>
                反转链表的三步骤
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <!-- Step 1 -->
                <div class="step-card">
                    <div class="step-content">
                        <div class="text-center mb-4">
                            <i class="fas fa-play-circle text-4xl text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">1. 初始化指针</h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i><code class="bg-gray-100 px-2 py-1 rounded">prev</code> = null</li>
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i><code class="bg-gray-100 px-2 py-1 rounded">current</code> = head</li>
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i><code class="bg-gray-100 px-2 py-1 rounded">next</code> 用于暂存</li>
                        </ul>
                    </div>
                </div>

                <!-- Step 2 -->
                <div class="step-card">
                    <div class="step-content">
                        <div class="text-center mb-4">
                            <i class="fas fa-sync-alt text-4xl text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">2. 遍历链表</h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i>保存下一节点</li>
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i>反转当前指针</li>
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i>移动到下一个</li>
                        </ul>
                    </div>
                </div>

                <!-- Step 3 -->
                <div class="step-card">
                    <div class="step-content">
                        <div class="text-center mb-4">
                            <i class="fas fa-check-circle text-4xl text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">3. 完成反转</h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i>current = null</li>
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i>prev 成为新头</li>
                            <li><i class="fas fa-chevron-right text-pink-500 mr-2"></i>返回 prev</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <!-- Code Implementation -->
        <div class="content-card rounded-2xl overflow-hidden mb-12">
            <h2 class="text-3xl font-bold p-8 pb-4">
                <i class="fas fa-code mr-3 text-purple-600"></i>
                代码实现
            </h2>
            
            <div class="code-block mx-8 mb-8">
                <div class="code-header">
                    <span><i class="fab fa-java mr-2"></i>Java</span>
                    <button class="text-gray-400 hover:text-white transition-colors">
                        <i class="fas fa-copy"></i>
                    </button>
                </div>
                <pre class="p-6 overflow-x-auto"><code><span class="keyword">public class</span> <span class="type">LinkedListReverseDemo</span> {

    <span class="comment">// 链表节点类</span>
    <span class="keyword">static class</span> <span class="type">ListNode</span> {
        <span class="keyword">int</span> val;
        <span class="type">ListNode</span> next;

        <span class="function">ListNode</span>(<span class="keyword">int</span> val) {
            <span class="keyword">this</span>.val = val;
            <span class="keyword">this</span>.next = <span class="keyword">null</span>;
        }
    }

    <span class="comment">// 反转链表</span>
    <span class="keyword">public static</span> <span class="type">ListNode</span> <span class="function">reverseList</span>(<span class="type">ListNode</span> head) {
        <span class="type">ListNode</span> prev = <span class="keyword">null</span>; <span class="comment">// 初始时，前一个节点为空</span>
        <span class="type">ListNode</span> current = head; <span class="comment">// 当前节点从头节点开始</span>

        <span class="keyword">while</span> (current != <span class="keyword">null</span>) {
            <span class="type">ListNode</span> next = current.next; <span class="comment">// 暂时保存下一个节点</span>
            current.next = prev; <span class="comment">// 反转当前节点的指针</span>
            prev = current; <span class="comment">// 更新前一个节点为当前节点</span>
            current = next; <span class="comment">// 移动到下一个节点</span>
        }

        <span class="keyword">return</span> prev; <span class="comment">// 返回新的头节点</span>
    }

    <span class="comment">// 打印链表</span>
    <span class="keyword">public static void</span> <span class="function">printList</span>(<span class="type">ListNode</span> head) {
        <span class="type">ListNode</span> current = head;
        <span class="keyword">while</span> (current != <span class="keyword">null</span>) {
            System.out.print(current.val + <span class="string">" "</span>);
            current = current.next;
        }
        System.out.println();
    }

    <span class="keyword">public static void</span> <span class="function">main</span>(<span class="type">String</span>[] args) {
        <span class="comment">// 创建一个链表：1 -> 2 -> 3 -> 4 -> 5</span>
        <span class="type">ListNode</span> head = <span class="keyword">new</span> <span class="type">ListNode</span>(<span class="number">1</span>);
        head.next = <span class="keyword">new</span> <span class="type">ListNode</span>(<span class="number">2</span>);
        head.next.next = <span class="keyword">new</span> <span class="type">ListNode</span>(<span class="number">3</span>);
        head.next.next.next = <span class="keyword">new</span> <span class="type">ListNode</span>(<span class="number">4</span>);
        head.next.next.next.next = <span class="keyword">new</span> <span class="type">ListNode</span>(<span class="number">5</span>);

        <span class="comment">// 打印原链表</span>
        System.out.println(<span class="string">"原链表:"</span>);
        print